﻿using System.ComponentModel;
using System.Windows;
using ORMAC.UI.WPF.ViewModel;
using System;

namespace ORMAC.UI.WPF.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class SQLConnectionForm : Window,INotifyPropertyChanged,IWizardForm
    {
        //private SqlConnectionString _test;

        public SQLConnectionForm(SQLConnectionViewModel vm)
        {
            this.DataContext = vm;
            InitializeComponent();
            
        }

        public SqlConnectionString ConnectionString
        {
            get { return new SqlConnectionString( (DataContext as SQLConnectionViewModel).OrmacConfig.ConnectionString); }
            set
            {
                (DataContext as SQLConnectionViewModel).OrmacConfig.ConnectionString = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ConnectionString"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void buttonNext_Click(object sender, RoutedEventArgs e)
        {
            //Load database objects
            (DataContext as SQLConnectionViewModel).OrmacConfig.LoadDBObjects();
            this.result = WizardFormResult.Next;
            this.Close();
        }

        private WizardFormResult result;
        public WizardFormResult Result
        {
            get
            {
                return result;
            }
        }
        public WizardFormResult ShowWizardPage()
        {
            try
            {
                this.ShowDialog();
                return result;
            }
            catch (Exception ex)
            {
                var res=MessageBox.Show("Could not connect to database. Do you want to change the settings and retry?","Database connection error",MessageBoxButton.YesNo,MessageBoxImage.Exclamation);
                if (res == MessageBoxResult.No)
                    return WizardFormResult.Abort;
                else
                    return WizardFormResult.Retry;
            }
        }

        private void buttonBack_Click(object sender, RoutedEventArgs e)
        {
            this.Hide();
            this.result = WizardFormResult.Back;
        }

        private void buttonFinish_Click(object sender, RoutedEventArgs e)
        {
            result = WizardFormResult.Finish;
            this.Close();

        }

        private void buttonAbort_Click(object sender, RoutedEventArgs e)
        {
            result = WizardFormResult.Abort;
            this.Close();

        }

    }
}
